package org.noote.libs.widget;

import javax.microedition.khronos.opengles.GL10;

import org.noote.libs.font.GLText;

public class Widget_Label extends Widget_Window {

	public static final int _ALIGN_LEFT = 0, _ALIGN_CENTER = 1,  _ALIGN_LEFTCENTER = 2, _ALIGN_TOPCENTER = 3;
	
	protected int _iTextAlign = _ALIGN_LEFTCENTER;
	protected int _iTextPosition_X = 0, _iTextPosition_Y = 0;
	protected int _iTextBox_X = 64, _iTextBox_Y = 64;

	protected String _sText = "";
	
	protected GLText _fontText = null;
	protected float _colorText[] = {0.3f, 0.62f, 1, 1};
	protected float _colorSelectedText[] = {0.7f, 1, 0.7f, 1};
	protected float _colorDisabledText[] = {0.65f, 0.65f, 0.7f, 1};
	
	public Widget_Label(Widget_Window parent) {
		super(parent);
	}
	
	public Widget_Label(Widget_Window parent, WidgetTheme theme) {
		super(parent, theme);
	}
	
	@Override
	public void create()
	{
		super.create();
		
		if(_theme != null)
		{
			_colorText = _theme.colorTextDefault;
			_colorSelectedText = _theme.colorTextSelected;
			_colorDisabledText = _theme.colorTextDisabled;
		}
		
		setTextFont(_theme.fontRegular16);
		setTextPosition(0, 0);
		setTextAlign(_ALIGN_LEFTCENTER);
	}

	public void setText(String sText)
	{
		_sText = sText;
	}
	
	public void setTextAlign(int iAlign)
	{
		_iTextAlign = iAlign;
	}
	public void setTextPosition(int iX, int iY) {
		setTextPosition(iX, iY, _iTextPosition_X, _iTextPosition_Y);
	}
	public void setTextPosition(int iX, int iY, int iBX, int iBY) {
		_iTextBox_X = iX;
		_iTextBox_Y = iY;
		
		_iTextPosition_X = iX;
		_iTextPosition_Y = iY;
	}
	public void setTextColor(float r, float g, float b) {
		setTextColor(r, g, b, 1.0f);
	}
	public void setTextColor(float r, float g, float b, float a) {
		float color[] = {r, g, b, a};
		setTextColor(color);
	}
	public void setTextColor(float color[]) {
		_colorText = color;
		refresh();
	}
	public void setTextFont(GLText font) {
		_fontText = font;
	}
	
	@Override
	protected void _drawElements(GL10 gl, float fDepth)
	{
		super._drawElements(gl, fDepth);
		
		if (_fontText != null && _sText.length() > 0) {
		    
			if(_bEnabled && _bSelected)
				_fontText.begin(_colorSelectedText[0], _colorSelectedText[1], _colorSelectedText[2], _colorSelectedText[3]);
			else if(!_bEnabled)
				_fontText.begin(_colorDisabledText[0], _colorDisabledText[1], _colorDisabledText[2], _colorDisabledText[3]);
			else
				_fontText.begin(_colorText[0], _colorText[1], _colorText[2], _colorText[3]);
			if(_iTextAlign == _ALIGN_LEFT) _fontText.draw(_sText, _iTextPosition_X, _iTextPosition_Y);
			else if(_iTextAlign == _ALIGN_LEFTCENTER) _fontText.drawCY(_sText, _iTextPosition_X, _iTextPosition_Y);
			else if(_iTextAlign == _ALIGN_TOPCENTER) _fontText.drawCX(_sText, _iTextPosition_X, _iTextPosition_Y);
			else _fontText.drawC(_sText, _iTextPosition_X, _iTextPosition_Y);
			_fontText.end();		    
		}
	}
}
